//
//  SGJRegisterViewController.m
//  Sewage
//
//  Created by 宗庆猛 on 2017/5/31.
//  Copyright © 2017年 宗庆猛. All rights reserved.
//

#import "SGJRegisterViewController.h"
#import "SGJRegisterSetTheDataViewController.h"

#import "SGJNetworkTools.h"

@interface SGJRegisterViewController ()
// 拿到三个View只是为了切圆角
@property (weak, nonatomic) IBOutlet UIView *RegisterAccountNumberView;
@property (weak, nonatomic) IBOutlet UIView *RegisterVerificationCodeView;
@property (weak, nonatomic) IBOutlet UIView *SetLoginPasswordView;
@property (weak, nonatomic) IBOutlet UIButton *SignUpNowButton;
@property (weak, nonatomic) IBOutlet UITextField *InputMobilePhoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *EnterConfirmationCodeTextField;
@property (weak, nonatomic) IBOutlet UITextField *SetTheLoginPasswordTextField;

@end

@implementation SGJRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册通知监听TextField的编辑，编辑状态下整个View上移动
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    self.view.backgroundColor = [UIColor colorWithWhite:.95 alpha:1];
    self.RegisterAccountNumberView.layer.cornerRadius = 22;
    self.RegisterVerificationCodeView.layer.cornerRadius = 22;
    self.SetLoginPasswordView.layer.cornerRadius = 22;
    self.SignUpNowButton.layer.cornerRadius = 22;
    
    self.InputMobilePhoneNumberTextField.borderStyle = UITextBorderStyleNone
    ;
    self.EnterConfirmationCodeTextField.borderStyle = UITextBorderStyleNone
    ;
    self.SetTheLoginPasswordTextField.borderStyle = UITextBorderStyleNone
    ;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 返回按钮
- (IBAction)returnButton:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
// 获取验证码，需要网络请求
- (IBAction)GetVerificationCodeButton:(id)sender {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"tel"] = self.InputMobilePhoneNumberTextField.text;
    
    
    [[SGJNetworkTools sharedNetworkTool] getVerifyCodeWithURLString:kGetSmsValidateCodeUrl parameters:params method:@"POST" callBack:^(id responseObject) {
        NSLog(@"获取验证码%@",responseObject);
    }];
}
// 点击注册按钮
- (IBAction)SignUpNowButton:(id)sender {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //{tel:1875662,pwd:111111,checkCode:aeto }
    params[@"tel"] = self.InputMobilePhoneNumberTextField.text;
    params[@"pwd"]= self.SetTheLoginPasswordTextField.text;
    params[@"checkCode"]  = self.EnterConfirmationCodeTextField.text;
    
    [[SGJNetworkTools sharedNetworkTool] registerWithURLString:kRegisterUrl parameters:params method:@"POST" callBack:^(id responseObject) {
        NSLog(@"注册%@",responseObject);
    }];
    
    
    SGJRegisterSetTheDataViewController *registerSetTheDataViewController = [[SGJRegisterSetTheDataViewController alloc]init];
     [self presentViewController:registerSetTheDataViewController animated:YES completion:nil];
    //[self.navigationController pushViewController:registerSetTheDataViewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
// 写一个方法取到编辑状态下的textfield
- (void)getIsEditingView:(UIView *)rootView {
    for (UIView *subView in rootView.subviews) {
        if ([subView isKindOfClass:[UITextField class]]) {
            if (((UITextField *)subView).isEditing) {
                self.InputMobilePhoneNumberTextField = subView;
                return;
            }else if (((UITextField *)subView).isEditing){
                self.EnterConfirmationCodeTextField = subView;
                return;
            }else if (((UITextField *)subView).isEditing){
                self.SetTheLoginPasswordTextField = subView;
                return;
            }
        }
        [self getIsEditingView:subView];
    }
}
// 计算已经拿到的textfield在屏幕中的位置
- (CGFloat)screenViewYValue:(UIView *)textfield {
    CGFloat y = 0;
    for (UIView *view = textfield; view; view = view.superview) {
        y += view.frame.origin.y;
        if ([view isKindOfClass:[UIScrollView class]]) {
            // 如果父视图是UIScrollView则要去掉内容滚动的距离
            UIScrollView* scrollView = (UIScrollView*)view;
            y -= scrollView.contentOffset.y;
        }
    }
    return y;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

// 拿到的这个位置来进行视图上移处理, 也就是通知执行的方法
- (void)keyboardWillShow:(NSNotification *)noti {
    // 拿到正在编辑中的textfield
    [self getIsEditingView:self.view];
    // textfield的位置
    CGFloat viewY = [self screenViewYValue:self.SetTheLoginPasswordTextField];
    // 键盘的Y值
    NSDictionary *userInfo = [noti userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardEndY = value.CGRectValue.origin.y;
    // 动画
    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:duration.doubleValue animations:^{
        if (viewY+30 > keyboardEndY) {
            CGRect rect = self.view.frame;
            rect.origin.y += keyboardEndY - (viewY+120);
            self.view.frame = rect;
        }
    }];
}

- (void)keyboardWillHide:(NSNotification *)noti {
    CGRect rect = self.view.frame;
    rect.origin.y = 0;
    self.view.frame = rect;
}
// 视图销毁移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


@end
